package com.hboxs.asl.controller.app.product;

import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.Category;
import com.hboxs.asl.service.CategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller(value = "appCategoryController")
@RequestMapping(value = "/app/category")
public class CategoryControl extends BaseController {

    @Resource(name = "categoryServiceImpl")
    private CategoryService categoryService;

    /**
     * 获取总分类
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage list() {
        try {
            List<Category> categories = categoryService.findAll();
            if(categories.size() > 0) {
                return JsonMessage.success(JsonMessage.optimiseObj(categories, "name", "id", "imageURL"));
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

}
